﻿using System;

namespace Guia.Elements
{
    using System.Windows.Automation;
    using Guia.Locator;
    using System.Diagnostics.Contracts;

    /// <summary>
    /// This class represents the Header of a DataGrid.
    /// </summary>
    public class DataGridHeader : UiElement
    {
        /// <summary>
        /// Initializes a new instance of the DataGridHeader class. 
        /// </summary>
        /// <param name="automationElement">The AutomationElement representating the DataGridHeader.</param>
        public DataGridHeader(AutomationElement automationElement)
            : base(automationElement)
        {
        }

        /// <summary>
        /// Gets the default-condition to find a control by its type.
        /// </summary>
        public static SearchCondition ControlTypeCondition
        {
            get { return SearchCondition.ByControlType(ControlType.Header); }
        }

        /// <summary>
        /// Gets the number of column of this DataGridHeader.
        /// </summary>
        public int ColumnCount
        {
            get
            {
                return this.GetAll<DataGridHeaderItem>().Length;
            }
        }

        /// <summary>
        /// Gets all columns (DataGridHeaderItem instances) of this header. 
        /// Attention: Depending on the concrete WPF-control (ListView / DataGrid), the indexed array may be organized in another way (0-based or not).
        /// </summary>
        public DataGridHeaderItem[] HeaderColumns
        {
            get
            {
                return this.GetAll<DataGridHeaderItem>();
            }
        }

        /// <summary>
        /// Gets the DataGridHeaderItem of the given column of this header.
        /// </summary>
        /// <param name="columnName">The name of the desired column.</param>
        /// <returns>A DataGridHeaderItem representating the desired header-column.</returns>
        /// <exception cref="ArgumentNullException">This exception is thrown if columnName is null</exception>
        public DataGridHeaderItem this[string columnName]
        {
            get
            {
                Contract.Requires<ArgumentNullException>(columnName != null);
                return this.Get<DataGridHeaderItem>(SearchCondition.ByName(columnName));
            }
        }

        /// <summary>
        /// Gets the DataGridHeaderItem of the given column of this header.
        /// Attention: Depending on the concrete WPF-control (ListView / DataGrid), the columnId may be implemented in another way (0-based or not).
        /// </summary>
        /// <param name="columnId">The id of the desired column.</param>
        /// <returns>A DataGridHeaderItem representating the desired header-column.</returns>
        public DataGridHeaderItem this[int columnId]
        {
            get
            {
                return this.GetAll<DataGridHeaderItem>()[columnId];
            }
        }
    }
}
